package com.hist.utils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @auther Ren Gaoshuai
 * @date 2021-04-02 9:51
 **/
public class MyBatisUtils {
    private static InputStream inputStream;
    private static SqlSessionFactory sqlSessionFactory;

    static{

        try {
            String resource = "mybatis-config.xml";
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 返回一个SQLSession对象
     * @return
     */
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }


}
